package com.terren.statistic.execute.excel;

import java.io.File;
import java.util.List;

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.terren.statistic.common.entity.Documents;
import com.terren.statistic.common.util.ExcelUtil;
import com.terren.statistic.common.util.StringUtil;

public class DocExport2Excel {
	/**
	 * 
	 * @param dataList
	 * @param name
	 */
	public String createExcel(List<?> dataList, String name, String path) throws Exception {
		//标题  来源站点 发布时间  PV UV 文章内容  URL

		/** 创建Excel的工作书册 Workbook,对应到一个excel文档 **/
		XSSFWorkbook excelBook = new XSSFWorkbook();

		/** 创建Excel的工作sheet,对应到一个excel文档的tab **/
		XSSFSheet sheet = excelBook.createSheet("sheet1");

		/** 设置excel每列宽度 **/
		sheet.setColumnWidth(0, 10000);
		sheet.setColumnWidth(1, 4000);
		sheet.setColumnWidth(2, 5000);
		sheet.setColumnWidth(3, 3000);
		sheet.setColumnWidth(4, 3000);
		sheet.setColumnWidth(5, 20000);
		sheet.setColumnWidth(6, 10000);

		/** 创建单元格样式，居中 **/
		XSSFCellStyle style = excelBook.createCellStyle();
		
		style.setAlignment(HorizontalAlignment.CENTER);
		style.setVerticalAlignment(VerticalAlignment.CENTER);

		/** ============================== 第一行 标题 ============================== **/
		/** 创建Excel的sheet的一行 **/
		XSSFRow row = sheet.createRow(0);

		/** 创建一个Excel的单元格 **/
		XSSFRichTextString stringValue = null;

		XSSFCell cell = row.createCell(0);
		cell.setCellStyle(style);
		stringValue = new XSSFRichTextString("标题");
		cell.setCellValue(stringValue);

		cell = row.createCell(1);
		cell.setCellStyle(style);
		stringValue = new XSSFRichTextString("来源站点");
		cell.setCellValue(stringValue);

		cell = row.createCell(2);
		cell.setCellStyle(style);
		stringValue = new XSSFRichTextString("发布时间");
		cell.setCellValue(stringValue);

		cell = row.createCell(3);
		cell.setCellStyle(style);
		stringValue = new XSSFRichTextString("浏览量");
		cell.setCellValue(stringValue);
		
		cell = row.createCell(4);
		cell.setCellStyle(style);
		stringValue = new XSSFRichTextString("访客数");
		cell.setCellValue(stringValue);

		cell = row.createCell(5);
		cell.setCellStyle(style);
		stringValue = new XSSFRichTextString("文章内容");
		cell.setCellValue(stringValue);

		cell = row.createCell(6);
		cell.setCellStyle(style);
		stringValue = new XSSFRichTextString("原文链接");
		cell.setCellValue(stringValue);

		/** ============================== 第二行 数据 ============================== **/
		if(dataList == null || dataList.isEmpty()){//无结果
			/** 新的一行 **/
			row = sheet.createRow(1);

			/** ==================== 列 ==================== **/
			cell = row.createCell(0);
			stringValue = new XSSFRichTextString("无结果");
			cell.setCellValue(stringValue);
		}else{
			int number = 0;
			for(Object obj : dataList){
				Documents doc = (Documents)obj;
				number ++;

				/** 新的一行 **/
				row = sheet.createRow(number);

				/** ==================== 列 ==================== **/
				/** 1列 title  **/
				cell = row.createCell(0);
				stringValue = new XSSFRichTextString(doc.getTitle());
				cell.setCellValue(stringValue);

				/** 2列 网站来源 **/
				cell = row.createCell(1);
				cell.setCellValue(doc.getCustomer());

				/** 3列 发布时间 **/
				cell = row.createCell(2);
				String pubTime = doc.getPublishDay();
				cell.setCellValue(pubTime);
				
				/** 4列 PV **/
				cell = row.createCell(3);
				cell.setCellValue(doc.getPv());

				/** 5列 UV **/
				cell = row.createCell(4);
				cell.setCellValue(doc.getPv());

				/** 6列 文章内容 **/
				cell = row.createCell(5);
				cell.setCellStyle(style);
				try {
					stringValue = new XSSFRichTextString(StringUtil.truncateAsText(doc.getContent()));
					cell.setCellValue(stringValue);
				}catch(Exception e) {
					e.printStackTrace();
					stringValue = new XSSFRichTextString(StringUtil.truncate(doc.getContent(), 32766));
					cell.setCellValue(stringValue);
				}
			
				/** 7列 原文链接**/
				cell = row.createCell(6);
				cell.setCellStyle(style);
				cell.setCellValue(doc.getUrl());
			}
		}

		ExcelUtil eu = new ExcelUtil();
		File excelFile = eu.writeFile(excelBook, name, path);
		String excelPath = excelFile.getAbsolutePath();
		return excelPath;
	}
}
